/*
 * Exit66JukeboxGui.java
 *
 * Created on April 12, 2006, 9:52 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.exit66.jukebox;

import java.lang.reflect.Method;
import javax.swing.*;

import snoozesoft.systray4j.*;

/**
 *
 * @author andyb
 */
public class Exit66JukeboxGui extends Exit66Jukebox implements SysTrayMenuListener {
    
    /** Creates a new instance of Exit66JukeboxGui */
    public Exit66JukeboxGui(boolean noLog) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
                        
        String osName = System.getProperty("os.name");
        
        if (osName.startsWith("Windows")) {
            createMenu();
        }
                
        start(noLog);
    }
    
        SysTrayMenu menu;
    
    private static final String[] toolTips =
    {
        "Exit 66 Jukebox"
    };
    
    // create icons
    static final SysTrayMenuIcon[] icons =
    {
        // the extension can be omitted
        new SysTrayMenuIcon( "exit66jb" )
    };
    
    protected String promptForInput(String question) {
        return javax.swing.JOptionPane.showInputDialog(null,
                question,
                "Exit66 JukeBox", javax.swing.JOptionPane.WARNING_MESSAGE);
    }
    
    void createMenu() {
        // create an exit item
        SysTrayMenuItem itemExit = new SysTrayMenuItem("Exit", "exit");
        itemExit.addSysTrayMenuListener(this);
        
        // create an about item
        SysTrayMenuItem itemAbout = new SysTrayMenuItem("About...", "about");
        itemAbout.addSysTrayMenuListener(this);
        
        SysTrayMenuItem itemPlayPause = new SysTrayMenuItem("Play/Pause", "playpause");
        itemPlayPause.addSysTrayMenuListener(this);
        
        SysTrayMenuItem itemStop = new SysTrayMenuItem("Skip Current Song", "stop");
        itemStop.addSysTrayMenuListener(this);
        
        SysTrayMenuItem itemAdmin = new SysTrayMenuItem("Admin Control Center", "admin");
        itemAdmin.addSysTrayMenuListener(this);
        
        SysTrayMenuItem itemStart = new SysTrayMenuItem("Launch Exit 66 Jukebox", "start");
        itemStart.addSysTrayMenuListener(this);
        
        // create the main menu
        icons[0].addSysTrayMenuListener(this);
        menu = new SysTrayMenu( icons[ 0 ], toolTips[ 0 ] );
        
        // insert items
        menu.addItem(itemExit);
        menu.addSeparator();
        menu.addItem(itemAbout);
        menu.addSeparator();
        menu.addItem(itemStop);
        menu.addItem(itemPlayPause);
        menu.addItem(itemAdmin);
        menu.addItem(itemStart);
    }
    
    public void menuItemSelected( SysTrayMenuEvent e ) {
        if( e.getActionCommand().equals( "exit" ) )
            WebServer.getWebServer().shutDown();
        else if (e.getActionCommand().equals("admin")) {
            try {
                openURL(getLocalUrl() + "admin/");
            } catch (Exception ex) {
                System.err.println(ex.getMessage());
            }
        }
        else if (e.getActionCommand().equals("start")) {
            try {
                openURL(getLocalUrl());
            } catch (Exception ex) {
                System.err.println(ex.getMessage());
            }
        } else if (e.getActionCommand().equals("about")) {
            String newLine = System.getProperty("line.separator");
            JOptionPane.showMessageDialog(null, "Exit 66 Jukebox v" + this.version + newLine + 
                    "(c) 2002-2006 Andrew Barilla" + newLine + newLine + 
                    "http://www.exit66.com/", 
                    "Exit 66 Jukebox", 
                    JOptionPane.INFORMATION_MESSAGE, 
                    new ImageIcon("exit66jb.png"));
        } else if (e.getActionCommand().equals("stop")) {
            if (!Options.inDemoMode()) 
                SoundPlayer.getSoundPlayer().stopSong();
            else 
                DemoSoundPlayer.getSoundPlayer().stopSong();
        } else if (e.getActionCommand().equals("playpause")) {
            if (!Options.inDemoMode()) 
                SoundPlayer.getSoundPlayer().playPause();
            else 
                DemoSoundPlayer.getSoundPlayer().playPause();            
        }
    }
    
    public void iconLeftClicked( SysTrayMenuEvent e ) {
        
    }
    
    public void iconLeftDoubleClicked( SysTrayMenuEvent e ) {
        openURL(getLocalUrl());
    }
    
    public static void openURL(String url) {
        
        String osName = System.getProperty("os.name");
        
        try {
            if (osName.startsWith("Mac OS")) {
                Class macUtils = Class.forName("com.apple.mrj.MRJFileUtils");
                Method openURL = macUtils.getDeclaredMethod("openURL",
                        new Class[] {String.class});
                        openURL.invoke(null, new Object[] {url});
            } else if (osName.startsWith("Windows"))
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
            else { // assume Unix or Linux
                String[] browsers = {
                    "firefox", "opera", "konqueror", "mozilla", "netscape" };
                    String browser = null;
                    for (int count = 0; count < browsers.length && browser == null; count++)
                        if (Runtime.getRuntime().exec(
                            new String[] {"which", browsers[count]}).waitFor() == 0)
                            browser = browsers[count];
                            if (browser == null)
                                throw new Exception("Could not find web browser.");
                            else
                                Runtime.getRuntime().exec(new String[] {browser, url});
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error attempting to launch web browser" + ":\n" + e.getLocalizedMessage());
        }
    }
    
    private String getLocalUrl() {
        int port = Options.getWebServerPort();
        
        if (port == 80) {
            return "http://localhost/";
        }
        else {
            return "http://localhost:" + String.valueOf(port) + "/";
        }
    }

}